
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Program extends CI_Controller {

	public function __construct(){
		parent::__construct();
		$this->load->helper('download');
		$this->load->model('wus');
		$this->load->helper('form');
		$this->load->helper('url');
		$this->load->helper('file');
		$this->load->model($this->session->userdata('formodel'));
		$this->load->library('variable');
	}

	public function getDownload(){

		$path = $_GET['path'];
		$file = $_GET['file'];

		$data = file_get_contents($path .'/'.$file); // Read the file's contents
		$name = $file;

		force_download($name, $data);
		echo $data;
	}

	public function index(){

		 $this->load->view('layouts/header',$this->variable->load_data($this->session->userdata('title')));

	    $this->load->view($this->session->userdata('formain'),array(
	    	'mods'      => $this->wus->getMod(),
	    	'glLookups' => $this->wus->getLookupValues()
	    	));

	    $this->load->view('layouts/footer');

	}

	// public function saveForm(){
	// 	$dir;
	// 	$mod_menu_name;
	// 	foreach($this->wus->getModbyID($this->input->post('module-id')) as $mod){
	// 		$dir = $mod->dir_path;
	// 	}
	// 	foreach ($this->wus->getModuleMenuID($this->input->post('mod_menu_id')) as $mmenu) {
	// 		$mod_menu_name = $mmenu->menu_name;
	// 	}
	// 	$fpath = $dir.'/'.$mod_menu_name.'/'.$this->input->post('prog_type').'/';
	// 	//echo $mod_menu_name;
 //        //$config['upload_path'] = $dir.'/'.$group_name;

 //        $config['upload_path'] = $fpath;
 //    	$config['allowed_types'] = 'application/octet-stream|php';
	// 	$this->load->library('upload', $config);
 //        if (!$this->upload->do_upload('upfile')) {
 //            $data = array('msg' => $this->upload->display_errors());
 //            echo json_encode($data);
 //        }else { //else, set the success message

 //            $data1 = $this->upload->data();
 //            $filepath = $data1['full_path'];
 //            $ena = $this->input->post('form-enabler');
	// 		$data = array(
	//     		':p_form_code' 		    => filter_var($this->input->post('form-code'), FILTER_SANITIZE_STRING),
	//     		':p_form_name' 		    => filter_var($this->input->post('form-name'), FILTER_SANITIZE_STRING),
	//     		':p_description'		=> filter_var($this->input->post('form-desc'), FILTER_SANITIZE_STRING),
	//     		':p_program_type'		=> filter_var($this->input->post('prog_type'), FILTER_SANITIZE_STRING),
	//     		':p_module_id'			=> filter_var($this->input->post('module-id'), FILTER_SANITIZE_STRING),
	//     		':p_menu_id'			=> filter_var($this->input->post('mod_menu_id'), FILTER_SANITIZE_STRING),
	//     		':p_exe_file_name'		=> $this->input->post('form-exe-name'),
	//     		':p_enable_flag'	 	=> ($ena == 'ok' ? 'Y' : 'N'),
	//     		':p_created_by'			=> 1
	//     	);
	// 		$q = $this->program_model->insertForm($data);
	// 		echo json_encode($q);
 //        }
		
    	
	// }
	public function saveForm(){
            $ena = $this->input->post('form-enabler');
			$data = array(
	    		':p_form_code' 		    => filter_var($this->input->post('form-code'), FILTER_SANITIZE_STRING),
	    		':p_form_name' 		    => filter_var($this->input->post('form-name'), FILTER_SANITIZE_STRING),
	    		':p_description'		=> filter_var($this->input->post('form-desc'), FILTER_SANITIZE_STRING),
	    		':p_program_type'		=> filter_var($this->input->post('prog_type'), FILTER_SANITIZE_STRING),
	    		':p_module_id'			=> filter_var($this->input->post('module-id'), FILTER_SANITIZE_STRING),
	    		':p_menu_id'			=> filter_var($this->input->post('mod_menu_id'), FILTER_SANITIZE_STRING),
	    		':p_exe_file_name'		=> $this->input->post('form-exe-name'),
	    		':p_enable_flag'	 	=> ($ena == 'ok' ? 'Y' : 'N'),
	    		':p_created_by'			=> $this->session->userdata('user_id')
	    	);
			$q = $this->program_model->insertForm($data);
			if($q['err'] == 1243){
				$module_name = $this->program_model->get_module_name($this->input->post('module-id'));
				touch("application/controllers/".$module_name[0]->dir_path."/".$this->input->post('form-exe-name'));
				chmod("application/controllers/".$module_name[0]->dir_path."/".$this->input->post('form-exe-name'), 0755);
				$controller = fopen("application/controllers/".$module_name[0]->dir_path."/".$this->input->post('form-exe-name'), "w") or die("Unable to open file!");
				$txt = "<?php if (!defined('BASEPATH')) {
							exit('No direct script access allowed');
						}

						class ".ucfirst(substr($this->input->post('form-exe-name'),0,-4))." extends CI_Controller {

							public function __construct() {
								parent::__construct();
								$this->load->model('wus');
								$this->load->model($this->session->userdata('formodel'));
								$this->load->library('upload');
								$this->load->library('variable');
							}

							public function index() {
								$this->load->view('layouts/header', $this->variable->load_data($this->session->userdata('title')));
								$this->load->view($this->session->userdata('formain'));
								$this->load->view('layouts/footer');
							}

							public function add_form() {
								$this->load->view($this->session->userdata('foradd'));
							}

							public function edit_form($id) {
								$this->load->view($this->session->userdata('foredit'));
							}

							public function view_form($id) {
								$this->load->view($this->session->userdata('forview'));
							}
						}";
				fwrite($controller, $txt2);
				fclose($controller);
				touch("application/models/".$module_name[0]->dir_path."/".substr($this->input->post('form-exe-name'),0,-4)."_model.php");
				chmod("application/models/".$module_name[0]->dir_path."/".substr($this->input->post('form-exe-name'),0,-4)."_model.php", 0755);
				$model = fopen("application/models/".$module_name[0]->dir_path."/".substr($this->input->post('form-exe-name'),0,-4)."_model.php", "w") or die("Unable to open file!");
				$txt2 = "<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

						class ".ucfirst(substr($this->input->post('form-exe-name'),0,-4))." extends CI_Model
						{
						    public function __construct()
						    {
						        parent::__construct();
						    }
						}";
				fwrite($model, $txt2);
				fclose($model);
			}
			
			echo json_encode($q);


	}

	public function searchmodmenu(){
		$query = $this->wus->getModuleMenu($this->input->post('mod_menu_id'));
		echo json_encode($query);
	}

	public function searchCode(){
		$code = $this->input->post('form-code');
		$query = $this->wus->getFormModuleMenu($code);
		if(empty($query)){
			echo json_encode('');
		}else {
			echo json_encode($query);
		}
	}

	public function submitFormFunction(){
		$data = array(
			':p_trx_type'		=> 'I',
			':p_form_id' 		=> filter_var($this->input->post('form-id'), FILTER_SANITIZE_STRING),
			':p_display_name' 	=> filter_var($this->input->post("form-disp-name"), FILTER_SANITIZE_STRING),
			':p_form_parameter' => filter_var($this->input->post("form-parameter"), FILTER_SANITIZE_STRING),
			':p_description' 	=> filter_var($this->input->post("form-description"), FILTER_SANITIZE_STRING),
			':p_menu_id' 		=> filter_var($this->input->post("form-menu"), FILTER_SANITIZE_STRING),
			':p_enable_flag' 	=> ($this->input->post("form-enabler") == 'ok' ? 'Y' : 'N'),
			':p_user_id' 		=> $this->session->userdata('user_id')
		);
		$this->wus->insertFormFunction($data);
	}

	public function getfile(){
		$id = $_POST['post_id'];
		$query = $this->wus->getPathpage($id);
		echo json_encode($query);
	}

	public function searchFormData()
	{
		$fcode = $this->input->post('form-code-s');
		$fname = $this->input->post('form-name-s');
		$mname = $this->input->post('module-name');
		$efn   = $this->input->post('exe-file-name-s');

		$query = $this->program_model->searchFomrData($fcode, $fname, $mname, $efn);
		echo json_encode($query);
	}

	public function editForm(){
		$ena = $this->input->post('form_enabler_e');
		$menu_id = $this->input->post('mod_menu_id_e');
		$menu_id_h = $this->input->post('mod_menu_id_hidden');
		$menu_id_val = ($menu_id != "" ? $menu_id : $menu_id_h);
		//echo $menu_id_val;
			// $data = array(
	  //   		':p_form_name' 		    => filter_var($this->input->post('form_name'), FILTER_SANITIZE_STRING),
	  //   		':p_description'		=> filter_var($this->input->post('form_desc'), FILTER_SANITIZE_STRING),
	  //   		':p_program_type'		=> filter_var($this->input->post('prog_type'), FILTER_SANITIZE_STRING),
	  //   		':p_module_id'			=> filter_var($this->input->post('module_id'), FILTER_SANITIZE_STRING),
	  //   		':p_menu_id'			=> filter_var($this->input->post('mod_menu_id'), FILTER_SANITIZE_STRING),
	  //   		':p_exe_file_name'		=> $this->input->post('form-exe-name'),
	  //   		':p_enable_flag'	 	=> ($ena == 'ok' ? 'Y' : 'N'),
	  //   		':p_created_by'			=> 1
	  //   	);
			// $q = $this->program_model->insertForm($data);
			// echo json_encode($q);

			$data = array(
	    		':p_form_name' 		    => filter_var($this->input->post('form_name_e'), FILTER_SANITIZE_STRING),
	    		':p_description'		=> filter_var($this->input->post('form_desc_e'), FILTER_SANITIZE_STRING),
	    		':p_program_type'		=> filter_var($this->input->post('prog_type_e'), FILTER_SANITIZE_STRING),
	    		':p_module_id'			=> filter_var($this->input->post('module_id_e'), FILTER_SANITIZE_STRING),
	    		':p_menu_id'			=> filter_var($menu_id_val, FILTER_SANITIZE_STRING),
	    		':p_exe_file_name'		=> filter_var($this->input->post('form_exe_name_e'), FILTER_SANITIZE_STRING),
	    		':p_enable_flag'	 	=> ($ena == 'ok' ? 'Y' : 'N'),
	    		':p_updated_by'			=> $this->session->userdata('user_id'),
				':p_form_id'			=> filter_var($this->input->post('form_id_e'), FILTER_SANITIZE_STRING)
	    	);
	    	$this->program_model->editFormData($data);

    	//echo json_encode($data);
		// $ena = $this->input->post('form_enabler_e');
		// if(!empty($this->input->post('upfile_e'))){
		// 	$dir;
		// 	foreach($this->wus->getModbyID($this->input->post('module_id_e')) as $mod){
		// 		$dir = $mod->dir_path;
		// 	}
		// 	// $module_menu;
		// 	// foreach($this->wus->getModMenu($this->input->post('')) as $mod_menu){
		// 	// 	$group_name = $mod_menu->group_name;
		// 	// }
		// 	get_file_info($dir.'/'.$this->input->post('form_exe_name_e'), $file_information);
		// 	echo $file_information;
		// 	$fpath = $dir.'/';
		// 	//echo $fpath;
	 //        //Configure
	 //        //set the path where the files uploaded will be copied. NOTE if using linux, set the folder to permission 777
	 //        //$config['upload_path'] = $dir.'/'.$group_name;

	 //        $config['upload_path'] = $dir.'/';
	 //    	// set the filter image types
	 //        $config['upload_path'] = $dir.'/';
	 //    	$config['allowed_types'] = 'application/octet-stream|php';
		// 	$this->load->library('upload', $config);
	 //        if (!$this->upload->do_upload('upfile')) {
	 //            $data = array('msg' => $this->upload->display_errors());
	 //            echo json_encode($data);
	 //        }else { //else, set the success message

	 //            $data1 = $this->upload->data();
	 //            $filepath = $data1['full_path'];
		// 		$data = array(
		//     		':p_form_name' 		    => filter_var($this->input->post('form_name_e'), FILTER_SANITIZE_STRING),
		//     		':p_description'		=> filter_var($this->input->post('form_desc_e'), FILTER_SANITIZE_STRING),
		//     		':p_module_id'			=> filter_var($this->input->post('module_id_e'), FILTER_SANITIZE_STRING),
		//     		':p_exe_file_name'		=> filter_var($this->input->post('form_exe_name_e'), FILTER_SANITIZE_STRING),
		//     		':p_enable_flag'	 	=> ($ena == 'ok' ? 'Y' : 'N'),
		//     		':p_updated_by'			=> 1,
		// 			':p_form_id'			=> filter_var($this->input->post('form-id_e'), FILTER_SANITIZE_STRING)
		//     	);
		//     	$this->program_model->editFormData($data);
		//     	//echo json_encode($data);

	 //        }
  //   	}else{
	 //        	$data = array(
		//     		':p_form_name' 		    => filter_var($this->input->post('form_name_e'), FILTER_SANITIZE_STRING),
		//     		':p_description'		=> filter_var($this->input->post('form_desc_e'), FILTER_SANITIZE_STRING),
		//     		':p_module_id'			=> filter_var($this->input->post('module_id_e'), FILTER_SANITIZE_STRING),
		//     		':p_exe_file_name'		=> filter_var($this->input->post('form_exe_name_e'), FILTER_SANITIZE_STRING),
		//     		':p_enable_flag'	 	=> ($ena == 'ok' ? 'Y' : 'N'),
		//     		':p_updated_by'			=> 1,
		// 			':p_form_id'			=> filter_var($this->input->post('form-id_e'), FILTER_SANITIZE_STRING)
		//     	);
		//     	$this->program_model->editFormData($data);
		//     	//echo json_encode($data);
	 //        }
		
	}

	public function add_form(){
		$this->load->view($this->session->userdata('foradd'),array(
	      'mods'       => $this->wus->getMod(),
	      'modulemenu' => $this->wus->getModuleMenus(),
	      'prog_type'	 => $this->program_model->getlookup('PROGRAM_TYPE'),
	    ));
	}

	public function edit_form($id){
		$query = $this->program_model->editData($id);
		$this->load->view($this->session->userdata('foredit'),array(
	      'mods'      => $this->wus->getMod(),
	      'edit_data' => $query,
	      'prog_type'	 => $this->program_model->getlookup('PROGRAM_TYPE'),
	    ));
	}
	public function view_form($id){
		$query = $this->program_model->editData($id);
		$this->load->view($this->session->userdata('forview'),array(
	      'mods'      => $this->wus->getMod(),
	      'view_data' => $query,
	      'prog_type'	 => $this->program_model->getlookup('PROGRAM_TYPE'),
	    ));
	}
}
